package com.test.clyq.daoImpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.dao.DataAccessException;

import com.test.clyq.dao.RegisterUsersDao;
import com.test.clyq.entity.RegisterUser;
import com.test.clyq.entity.Users;
import com.test.dao.hibernate.BaseDaoHibernate;
import com.test.util.FormatExceptionStack;
import com.test.util.Pagination;
import com.test.util.SessionFactoryUtil;

/**
 * RegisterUserDaoHibernate. <br>
 * 注册用户信息管理.
 * <p>
 * Copyright: Copyright (c) 2012-2-24 下午04:38:42
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * Author: liuxl@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
@SuppressWarnings("unchecked")
public class RegisterUserDaoHibernate extends BaseDaoHibernate implements RegisterUsersDao {

	/**
	 * 新增用户信息
	 * 
	 * @param user
	 *            用户实例
	 */
	public void addUser(RegisterUser user) {
		this.log.info("新增用户信息start");
		try {
			saveOrUpdateItem(user, null);
			this.log.debug("新增用户信息成功");
			this.log.info("新增用户信息end");
		}
		catch (DataAccessException e) {
			this.log.error("[Users]addNLUser发生错误！\n" + FormatExceptionStack.formatException(e));
		}

	}

	/**
	 * 根据用户ID删除用户
	 * 
	 * @param id
	 *            用户ID
	 * @param users
	 *            用户实例
	 * @return true or false
	 */
	public boolean delRegisterUserById(Long id, Users users) {
		this.log.info("根据用户ID删除用户start");
		try {
			RegisterUser user = (RegisterUser) getItem(RegisterUser.class, id);
			if (user != null) {
				removeItem(RegisterUser.class, id, users);
				this.log.debug("删除用户信息成功");
				this.log.info("根据用户ID删除用户end");
				return true;
			} else {
				this.log.debug("删除用户信息失败");
				return false;
			}
		}
		catch (DataAccessException e) {
			this.log.error("[Users]delNLUsersById发生错误！\n" + FormatExceptionStack.formatException(e));
			return false;
		}
	}

	/**
	 * 获得用户信息列表
	 * 
	 * @return 用户信息列表结果集合
	 */
	public List<RegisterUser> getRegisterUser() {
		this.log.info("获得用户信息列表start");

		try {
			List list = getItems(Users.class);
			if (list != null && list.size() > 0) {
				this.log.debug("获得列表数据");
				this.log.info("获得用户信息列表end");
				return list;
			} else {
				this.log.debug("表中没有数据");
				return new ArrayList<RegisterUser>();
			}
		}
		catch (DataAccessException e) {
			this.log.error("[Users]getUsers发生错误！\n" + FormatExceptionStack.formatException(e));
			return new ArrayList<RegisterUser>();
		}
	}

	/**
	 * 根据用户id获得用户信息
	 * 
	 * @param id
	 *            用户ID
	 * @return Users 用户实体
	 */
	public RegisterUser getRegisterUserById(Long id) {
		this.log.info("根据用户ID获得用户信息start");
		try {
			RegisterUser user = (RegisterUser) getItem(RegisterUser.class, id);
			if (user != null) {
				this.log.info("根据用户ID获得用户信息end");
				return user;
			} else {
				this.log.info("根据用户ID获得用户信息end");
				return new RegisterUser();
			}

		}
		catch (DataAccessException e) {
			this.log.error("[Users]getNLUsersById发生错误！\n" + FormatExceptionStack.formatException(e));
			return new RegisterUser();
		}
	}

	/**
	 * 根据用户名和密码获得用户信息
	 * 
	 * @param name
	 *            用户名称
	 * @param pwd
	 *            用户密码
	 * @return Users 用户实例
	 */
	public RegisterUser getUsersByNameAndPwd(String name, String pwd) {
		this.log.info("根据用户名和密码获得用户信息start");
		String sql = "from RegisterUser as nlusers where nlusers.usersLoginName=:name and nlusers.usersPassword=:pwd";
		SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createQuery(sql.toString());
			query.setString("name", name);
			query.setString("pwd", pwd);
			List list = query.list();
			if (list != null && list.size() > 0) {
				this.log.debug("获得列表数据");
				this.log.info("根据用户名和密码获得用户信息end");
				RegisterUser user = (RegisterUser) list.get(0);
				return user;
			} else {
				this.log.debug("表中没有数据");
				return null;
			}
		}
		catch (DataAccessException e) {
			this.log.error("[Users]getUsersByNameAndPwd发生错误！\n" + FormatExceptionStack.formatException(e));
			return null;
		}
		finally {
			SessionFactoryUtil.isClose(session);
		}
	}

	/**
	 * 保存或更新用户信息
	 * 
	 * @param user
	 *            用户实例
	 */
	public void saveOrUpdate(RegisterUser user) {
		this.log.info("保存或更新用户信息start");
		try {
			saveOrUpdateItem(user, null);
			this.log.debug("保存用户信息成功");
			this.log.info("保存或更新用户信息end");
		}
		catch (DataAccessException e) {
			this.log.error("[Users]saveOrUpdate发生错误！\n" + FormatExceptionStack.formatException(e));
		}

	}

	/**
	 * 获得用户列表（分页使用）
	 * 
	 * @return 用户信息列表结果集合
	 */
	public List<RegisterUser> userList(Pagination pagination) {
		this.log.info("获得用户信息列表start");
		String sql = "from RegisterUser as nlusers  order by nlusers.id desc";
		SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
			Query query = session.createQuery("select count(*) " + sql);
			List listCount = query.list();
			if (listCount != null && !listCount.isEmpty()) {
				pagination.setTotalRows(((Integer) listCount.get(0)).intValue()); // 获得记录总数
			} else {
				pagination.setTotalRows(0);
			}

			Query query1 = session.createQuery(sql.toString());
			query1.setFirstResult(pagination.getStartIndex());
			query1.setMaxResults(pagination.getNumPerPage());
			List list = query1.list();
			if (list != null && list.size() > 0) {
				this.log.debug("获得列表数据");
				this.log.info("获得用户信息列表end");
				return list;
			} else {
				this.log.debug("表中没有数据");
				return new ArrayList<RegisterUser>();
			}
		}
		catch (DataAccessException e) {
			this.log.error("[Users]userList发生错误！\n" + FormatExceptionStack.formatException(e));
			return new ArrayList<RegisterUser>();
		}
		finally {
			SessionFactoryUtil.isClose(session);
		}
	}

}
